package cn.itcast.musicplayer;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static SeekBar sb;
    private static TextView tv_progress, tv_total;
    //   private ObjectAnimator animator;
    private MusicService.MusicControl musicControl ;
    MyServiceCon conn;
    Intent intent;
    private boolean isUnbind= false; // 记录服务是否解绑
    private ObjectAnimator animator;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    public static Handler handler= new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            Bundle bundle = msg.getData();
            int duration = bundle.getInt("duration");
            int currentPosition = bundle.getInt("currentPosition");
            sb.setMax(duration);
            sb.setProgress(currentPosition);
            int minute = duration/1000/60;
            int second = duration/1000%60;
            String strMinute =null;
            String strSecond = null;
            if(minute<9) {
                strMinute = "0"+minute;
            }else {
                strMinute = ""+minute;
            }
            if(second<9){
                strSecond ="0"+second;
            }else {
                strSecond =""+second;
            }
            tv_total.setText(strMinute+":"+strSecond);


            minute = currentPosition/1000/60;
            second = currentPosition/1000%60;

            if(minute<9) {
                strMinute = "0"+minute;
            }else {
                strMinute = ""+minute;
            }
            if(second<9){
                strSecond ="0"+second;
            }else {
                strSecond =""+second;
            }
            tv_progress.setText(strMinute+":"+strSecond);
            return true;
        }
    });
    private void init() {
        tv_progress = findViewById(R.id.tv_progress);
        tv_total = findViewById(R.id.tv_total);
        sb = findViewById(R.id.sb);
        findViewById(R.id.btn_play).setOnClickListener(this);
        findViewById(R.id.btn_pause).setOnClickListener(this);
        findViewById(R.id.btn_continue_play).setOnClickListener(this);
        findViewById(R.id.btn_exit).setOnClickListener(this);

        intent = new Intent(this, MusicService.class);
        conn = new MyServiceCon();
        bindService(intent,conn,BIND_AUTO_CREATE);

        // 响应进度调整的事件
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO 进度调整
                int progress =seekBar.getProgress();
                musicControl.seekTo(progress);
            }
        });
        ImageView iv_music = findViewById(R.id.iv_music);
        animator = ObjectAnimator.ofFloat(iv_music, "rotation", 0f, 360.0f);
        animator.setDuration(10000);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(-1);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_play:
                //TODO1 播放音乐
                musicControl.play();
                animator.start();
                break;
            case R.id.btn_pause:
                // TODO2 暂停音乐
                musicControl.pausePlay();
                animator.pause();
                break;
            case  R.id.btn_exit:
                unbind(isUnbind);
                isUnbind = true;
                finish();
                break;
            case R.id.btn_continue_play:
                // TODO3 继续播放
                musicControl.continuePlay();
                animator.start();
                break;
        }
    }

    private void unbind(boolean isUnbind) {
        if(!isUnbind){
            musicControl.pausePlay();
            unbindService(conn);
            stopService(intent);
        }
    }


    class MyServiceCon implements ServiceConnection{
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //TODO 0 获取服务接口
            musicControl=(MusicService.MusicControl)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicControl=null;
        }
    }
}